package com.common.datasource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataSourceUtil {

    private Connection connection;

    private String url = "jdbc:mysql://39.99.227.145:3306/db_mi";
    private String driver = "com.mysql.cj.jdbc.Driver";
    private String user = "root";
    private String pwd = "123456";

    /**
     * 数据库连接
     */
    public void init(){
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,user,pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    //    System.out.println("链接成功");
    }

    public Connection getConnection() {
        return connection;
    }


    /**
     * 数据库关闭
     */
    public void close(){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
